package com.hzeport.bgs22emduty.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzeport.bgs22emduty.config.login.UserInfoDetailVO;
import com.hzeport.bgs22emduty.dto.*;
import com.hzeport.bgs22emduty.entity.*;
import com.hzeport.bgs22emduty.entity.DutyScheduleInfo;
import com.hzeport.bgs22emduty.enums.DeleteFlagEnum;
import com.hzeport.bgs22emduty.enums.IsDutyTypeEnum;
import com.hzeport.bgs22emduty.enums.IsSendEnum;
import com.hzeport.bgs22emduty.enums.SmsEnum;
import com.hzeport.bgs22emduty.outbound.*;
import com.hzeport.bgs22emduty.service.SecurityLogService;
import com.hzeport.bgs22emduty.util.MessageSendUtil;
import com.hzeport.bgs22emduty.vo.DutyScheduleExchange.DutyScheduleExchangeListVO;
import com.hzeport.bgs22emduty.vo.DutyScheduleExchange.DutyScheduleExchangeSaveVO;
import com.hzeport.bgs22emduty.vo.DutyScheduleExchange.DutyScheduleExchangedetailVO;
import com.hzeport.bgs22emduty.vo.ParamDuty.ParamDutyListVO;
import com.hzeport.bgs22emduty.vo.ParamSmsTemplate.ParamSmsTemplateListVO;
import com.hzeport.bgs22emduty.common.base.BaseEntity;
import com.hzeport.bgs22emduty.common.base.BaseResult;
import com.hzeport.bgs22emduty.common.utils.IpUtils;
import com.hzeport.bgs22emduty.common.base.BaseServiceImpl;
import com.hzeport.bgs22emduty.outbound.db.DutyScheduleExchangeRepositoryImpl;
import com.hzeport.bgs22emduty.service.DutyScheduleExchangeService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ${comments}
 *
 * @author code generator
 * @date 2022-12-02 11:21:59
 */
@Service
@Slf4j
public class DutyScheduleExchangeServiceImpl extends BaseServiceImpl<DutyScheduleExchangeRepositoryImpl, DutyScheduleExchange> implements DutyScheduleExchangeService {

    @Resource
    private DutyScheduleExchangeRepository dutyScheduleExchangeRepository;
    @Resource
    private DutyScheduleInfoRepository dutyScheduleInfoRepository;
    @Resource
    private BasePersonInfoRepository basePersonInfoRepository;
    @Resource
    private BaseDeptInfoRepository baseDeptInfoRepository;
    @Resource
    private MessageSendUtil messageSendUtil;
    @Resource
    private ParamSmsTemplateRepository paramSmsTemplateRepository;
    @Resource
    private SecurityLogService securityLogService;

    @Override
    public BaseResult<Page<DutyScheduleExchangeListVO>> list(DutyScheduleExchangeDTO exchangeDTO, UserInfoDetailVO userInfo) {
        LambdaQueryWrapper<DutyScheduleInfo> Wrapper = new LambdaQueryWrapper<>();
        if(!CollectionUtils.isEmpty(exchangeDTO.getTime())&&exchangeDTO.getTime().get(0)!=null){
            Wrapper.ge(DutyScheduleInfo::getDDay,LocalDate.parse(exchangeDTO.getTime().get(0)));
        }
        if(!CollectionUtils.isEmpty(exchangeDTO.getTime())&&exchangeDTO.getTime().get(1)!=null){
            Wrapper.le(DutyScheduleInfo::getDDay,LocalDate.parse(exchangeDTO.getTime().get(1)));
        }
        Wrapper.orderByDesc(DutyScheduleInfo::getDDay);
        Wrapper.eq(DutyScheduleInfo::getDUserno,userInfo.getPersonId());
        Page<DutyScheduleInfo> page=dutyScheduleInfoRepository.page(new Page(exchangeDTO.getPageNum(),exchangeDTO.getPageSize()),Wrapper);
        if(!CollectionUtils.isEmpty(page.getRecords())){
            DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("EEEE");
            List<DutyScheduleExchangeListVO> dutyScheduleExchangeListVOS=new ArrayList<>();
            for (DutyScheduleInfo record : page.getRecords()) {
                List<DutyScheduleExchange> duty=dutyScheduleExchangeRepository.list(new LambdaQueryWrapper<DutyScheduleExchange>()
                        .eq(DutyScheduleExchange::getDId,record.getId()));
                DutyScheduleExchangeListVO listvo = new DutyScheduleExchangeListVO();
                listvo.setDId(record.getId());
                listvo.setDeptName(record.getDDeptname());
                    if(record.getDDay().compareTo(LocalDate.now())>0){
                        listvo.setS("1");
                    }else {
                        listvo.setS("0");
                    }
                listvo.setExUsername(record.getDUsername());
                for (DutyScheduleExchange dutyScheduleExchange : duty) {
                    listvo.setExRecvUsername(dutyScheduleExchange.getExRecvUsername());
                }
                listvo.setDDay(record.getDDay()+"("+ofPattern.format(record.getDDay())+")");
                dutyScheduleExchangeListVOS.add(listvo);
            }
            Page<DutyScheduleExchangeListVO> pageIno=new Page();
            pageIno.setRecords(dutyScheduleExchangeListVOS);
            pageIno.setTotal(page.getTotal());
            return  BaseResult.ok(pageIno,"查询成功");
        }else {
            return  BaseResult.failed(null,"未查到值班信息");
        }
    }

    @Override
    public BaseResult<DutyScheduleExchangedetailVO> detail(String dId,UserInfoDetailVO userInfo) {
        List<DutyScheduleInfo> info = dutyScheduleInfoRepository.list(new LambdaQueryWrapper<DutyScheduleInfo>()
        .eq(BaseEntity::getId,dId).eq(DutyScheduleInfo::getDUserno,userInfo.getPersonId()));
        List<DutyScheduleExchange> list = dutyScheduleExchangeRepository.list(
                new LambdaQueryWrapper<DutyScheduleExchange>().eq(DutyScheduleExchange::getDId,dId));
        DutyScheduleExchangedetailVO exchangedetailVO = new DutyScheduleExchangedetailVO();
        for (DutyScheduleInfo scheduleInfo : info) {
            exchangedetailVO.setExUserno(scheduleInfo.getDUserno());
            exchangedetailVO.setExUsername(scheduleInfo.getDUsername());
            exchangedetailVO.setDDay(scheduleInfo.getDDay());
            exchangedetailVO.setDId(dId);
        }
        for (DutyScheduleExchange dutyScheduleExchange : list) {
            exchangedetailVO.setExRecvUserno(dutyScheduleExchange.getExRecvUserno());
            exchangedetailVO.setExRecvUsername(dutyScheduleExchange.getExRecvUsername());
        }
        return BaseResult.ok(exchangedetailVO,"获取详情成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResult<DutyScheduleExchange> saveData(DutyScheduleExchangeSaveVO dutyScheduleExchangeSaveVO, UserInfoDetailVO userInfo) throws Exception {
        if(StringUtils.isEmpty( dutyScheduleExchangeSaveVO.getExRecvUserno() )){
            return BaseResult.failed("更换的值班人工号不能为空");
        }
        DutyScheduleExchange exchange = new DutyScheduleExchange();
        BeanUtils.copyProperties(dutyScheduleExchangeSaveVO,exchange);
        DutyScheduleInfo byId = dutyScheduleInfoRepository.getById(dutyScheduleExchangeSaveVO.getDId());
        DutyScheduleExchange list = dutyScheduleExchangeRepository.getOne(new LambdaQueryWrapper<DutyScheduleExchange>()
                .eq(DutyScheduleExchange::getDId, dutyScheduleExchangeSaveVO.getDId())
        .ne(DutyScheduleExchange::getExStatus,"1"),false);

            if(list==null){
                exchange.setExDay(byId.getDDay());
                exchange.setExUserno(userInfo.getPersonId());
                exchange.setCreateBy(userInfo.getUserId());
                exchange.setCreateTime(LocalDateTime.now());
                exchange.setUpdateBy(userInfo.getUserId());
                exchange.setUpdateTime(LocalDateTime.now());
                exchange.setDelFlag(DeleteFlagEnum.NOT_DELETE.getValue());
                exchange.setExStatus("0");
                dutyScheduleExchangeRepository.save(exchange);
            }else {
                exchange.setId(list.getId());
                exchange.setExUserno(userInfo.getPersonId());
                exchange.setUpdateBy(userInfo.getUserId());
                exchange.setUpdateTime(LocalDateTime.now());
                exchange.setDelFlag(DeleteFlagEnum.NOT_DELETE.getValue());
                exchange.setExStatus("0");
                exchange.setRecVersion(list.getRecVersion());
                dutyScheduleExchangeRepository.updateById(exchange);
            }
        return BaseResult.ok(exchange,"申请调班成功");
    }

    private void doMessageSend4StartExchange(DutyScheduleExchange exchange, DutyScheduleInfo scheduleInfo) {
        try {
            // 调班申请人员
            List< ParamSmsTemplate > paramSmsTemplate = paramSmsTemplateRepository.list( new LambdaQueryWrapper< ParamSmsTemplate >()
                    .eq( ParamSmsTemplate::getDutyType , IsDutyTypeEnum.TBSQRY.getCode() )
                    .eq( ParamSmsTemplate::getTemplateCode , scheduleInfo.getCustomId() )
                    .eq( ParamSmsTemplate::getIsSend , IsSendEnum.IS.getCode() )
                    .eq( ParamSmsTemplate::getSmsType , SmsEnum.ADJUST_REMID.getCode() )
            );
            if ( ! CollectionUtils.isEmpty( paramSmsTemplate ) && ! StringUtils.isEmpty( scheduleInfo.getDPhone() ) ) {
                for ( ParamSmsTemplate template : paramSmsTemplate ) {
                    messageSendUtil.sendMessage( scheduleInfo.getDPhone() , template.getRemark() );
                }
            }
            // 调班确认人员
            paramSmsTemplate = paramSmsTemplateRepository.list( new LambdaQueryWrapper< ParamSmsTemplate >()
                    .eq( ParamSmsTemplate::getDutyType , IsDutyTypeEnum.TBQRRY.getCode() )
                    .eq( ParamSmsTemplate::getTemplateCode , scheduleInfo.getCustomId() )
                    .eq( ParamSmsTemplate::getIsSend , IsSendEnum.IS.getCode()  )
                    .eq( ParamSmsTemplate::getSmsType ,  SmsEnum.ADJUST_REMID.getCode() )
            );
            BasePersonInfo basePersonInfo =
                    basePersonInfoRepository.getOne( new LambdaQueryWrapper< BasePersonInfo >().eq( BasePersonInfo::getUserno , exchange.getExUserno() ) , false );
            if ( ! CollectionUtils.isEmpty( paramSmsTemplate ) && basePersonInfo != null && ! StringUtils.isEmpty( basePersonInfo.getPhone() ) ) {
                for ( ParamSmsTemplate template : paramSmsTemplate ) {
                    messageSendUtil.sendMessage( basePersonInfo.getPhone() , template.getRemark() );
                }
            }
        } catch ( Exception e ) {
            log.error("发送调班发起短信异常",e);
        }

    }

    @Override
    public BaseResult<Page<DutyScheduleExchangeListVO>> listture(DutyScheduleExchangeListTureDTO dto, UserInfoDetailVO userInfo) throws Exception {
        LambdaQueryWrapper<DutyScheduleExchange> Wrapper = new LambdaQueryWrapper<>();
        Wrapper.eq(DutyScheduleExchange::getExRecvUserno,userInfo.getPersonId());
        Wrapper.orderByDesc(DutyScheduleExchange::getExDay);
        List<DutyScheduleExchangeListVO> dutyScheduleExchangeListVOS=new ArrayList<>();
        Page<DutyScheduleExchange> page=dutyScheduleExchangeRepository.page(new Page(dto.getPageNum(),dto.getPageSize()),Wrapper);
        if(!CollectionUtils.isEmpty(page.getRecords())){
            for (DutyScheduleExchange record : page.getRecords()) {
                DutyScheduleExchangeListVO listVO = new DutyScheduleExchangeListVO();
                DutyScheduleInfo byId = dutyScheduleInfoRepository.getById(record.getDId());
                BeanUtils.copyProperties(record,listVO);
                listVO.setDDay(String.valueOf(record.getExDay()));
                if(!StringUtils.isEmpty(byId)){
                    listVO.setDeptName(byId.getDDeptname());
                }
                dutyScheduleExchangeListVOS.add(listVO);
            }
        }
        Page<DutyScheduleExchangeListVO> pageIno=new Page();
        pageIno.setRecords(dutyScheduleExchangeListVOS);
        pageIno.setTotal(page.getTotal());
        return  BaseResult.ok(pageIno,"查询成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public  BaseResult<DutyScheduleExchange> ture(String id, UserInfoDetailVO userInfo) throws Exception {
        DutyScheduleExchange exchange = dutyScheduleExchangeRepository.getById(id);
        exchange.setExStatus("1");
        exchange.setExConfrimTime(LocalDateTime.now());
        exchange.setUpdateBy(userInfo.getUserId());
        exchange.setUpdateTime(LocalDateTime.now());
        exchange.setDelFlag(DeleteFlagEnum.NOT_DELETE.getValue());
        dutyScheduleExchangeRepository.updateById(exchange);
        DutyScheduleInfo dutyScheduleInfo=dutyScheduleInfoRepository.getById(exchange.getDId());
        dutyScheduleInfo.setDUserno(userInfo.getUserId());
        List<BasePersonInfo> basePersonInfos = basePersonInfoRepository.list(new LambdaQueryWrapper<BasePersonInfo>()
                .eq(BasePersonInfo::getUserno,userInfo.getUserId()));
        if (CollectionUtils.isEmpty(basePersonInfos)){
            throw new Exception("无该工号对应人员!");
        }
        basePersonInfos=basePersonInfos.stream().filter(i->i.getDeptId()!=null&&baseDeptInfoRepository.getById(i.getDeptId())!=null).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(basePersonInfos)){
            throw new Exception("无该工号对应人员部门!");
        }
        BasePersonInfo tempBasePerson=basePersonInfos.stream().filter(i->"0".equals(i.getSideLine()))
                .findAny().orElse(null);
        if (tempBasePerson==null){
            tempBasePerson=basePersonInfos.get(0);
        }
        BasePersonInfo basePersonInfo=tempBasePerson;
        BaseDeptInfo baseDeptInfo=baseDeptInfoRepository.getById(basePersonInfo.getDeptId());
        dutyScheduleInfo.setDDeptname(baseDeptInfo.getDeptName());
        dutyScheduleInfo.setDDept(baseDeptInfo.getId());
        dutyScheduleInfo.setDPhone(basePersonInfo.getPhone());
        dutyScheduleInfo.setDUsername(basePersonInfo.getUsername());
        dutyScheduleInfo.setDUserno(basePersonInfo.getUserno());
        dutyScheduleInfo.setDUserjob(basePersonInfo.getDutypost());
        dutyScheduleInfo.setPhoneShort(basePersonInfo.getPhoneShort());
        dutyScheduleInfo.setDPhoneLand(basePersonInfo.getPhoneLand());
        dutyScheduleInfo.setRecVersion(baseDeptInfo.getRecVersion());
        dutyScheduleInfoRepository.updateById(dutyScheduleInfo);
//        updateDutyScheduleInfo(exchange);
        return  BaseResult.ok(exchange,"确认成功");
    }

    @Override
    public BaseResult<Page<DutyScheduleExchangeListVO>> listls(LSDutyScheduleExchangeDTO exchangeDTO) {
        LambdaQueryWrapper<DutyScheduleInfo> Wrapper = new LambdaQueryWrapper<>();
        if(!CollectionUtils.isEmpty(exchangeDTO.getTime())&&exchangeDTO.getTime().get(0)!=null){
            Wrapper.ge(DutyScheduleInfo::getDDay,LocalDate.parse(exchangeDTO.getTime().get(0)));
        }
        if(!CollectionUtils.isEmpty(exchangeDTO.getTime())&&exchangeDTO.getTime().get(1)!=null){
            Wrapper.le(DutyScheduleInfo::getDDay,LocalDate.parse(exchangeDTO.getTime().get(1)));
        }
        Wrapper.orderByDesc(DutyScheduleInfo::getDDay);
        Wrapper.eq(DutyScheduleInfo::getDUserno,exchangeDTO.getUserno());
        Page<DutyScheduleInfo> page=dutyScheduleInfoRepository.page(new Page(exchangeDTO.getPageNum(),exchangeDTO.getPageSize()),Wrapper);
        if(!CollectionUtils.isEmpty(page.getRecords())){
            DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("EEEE");
            List<DutyScheduleExchangeListVO> dutyScheduleExchangeListVOS=new ArrayList<>();
            for (DutyScheduleInfo record : page.getRecords()) {
                List<DutyScheduleExchange> duty=dutyScheduleExchangeRepository.list(new LambdaQueryWrapper<DutyScheduleExchange>()
                        .eq(DutyScheduleExchange::getDId,record.getId()));
                DutyScheduleExchangeListVO listvo = new DutyScheduleExchangeListVO();
                listvo.setDId(record.getId());
                listvo.setDeptName(record.getDDeptname());
                if(record.getDDay().compareTo(LocalDate.now())>0){
                    listvo.setS("1");
                }else {
                    listvo.setS("0");
                }
                listvo.setExUsername(record.getDUsername());
                for (DutyScheduleExchange dutyScheduleExchange : duty) {
                    listvo.setExRecvUsername(dutyScheduleExchange.getExRecvUsername());
                }
                listvo.setDDay(record.getDDay()+"("+ofPattern.format(record.getDDay())+")");
                dutyScheduleExchangeListVOS.add(listvo);
            }
            Page<DutyScheduleExchangeListVO> pageIno=new Page();
            pageIno.setRecords(dutyScheduleExchangeListVOS);
            pageIno.setTotal(page.getTotal());
            return  BaseResult.ok(pageIno,"查询成功");
        }else {
            return  BaseResult.failed(null,"未查到值班信息");
        }
    }

    @Override
    public BaseResult<DutyScheduleExchangedetailVO> detaills(LSDutyScheduleExchangeDeraillsDTO dto) {
        List<DutyScheduleInfo> info = dutyScheduleInfoRepository.list(new LambdaQueryWrapper<DutyScheduleInfo>()
                .eq(BaseEntity::getId,dto.getDId()).eq(DutyScheduleInfo::getDUserno,dto.getUserno()));
        List<DutyScheduleExchange> list = dutyScheduleExchangeRepository.list(
                new LambdaQueryWrapper<DutyScheduleExchange>().eq(DutyScheduleExchange::getDId,dto.getDId()));
        DutyScheduleExchangedetailVO exchangedetailVO = new DutyScheduleExchangedetailVO();
        for (DutyScheduleInfo scheduleInfo : info) {
            exchangedetailVO.setExUserno(scheduleInfo.getDUserno());
            exchangedetailVO.setExUsername(scheduleInfo.getDUsername());
            exchangedetailVO.setDDay(scheduleInfo.getDDay());
            exchangedetailVO.setDId(dto.getDId());
        }
        for (DutyScheduleExchange dutyScheduleExchange : list) {
            exchangedetailVO.setExRecvUserno(dutyScheduleExchange.getExRecvUserno());
            exchangedetailVO.setExRecvUsername(dutyScheduleExchange.getExRecvUsername());
        }
        return BaseResult.ok(exchangedetailVO,"获取详情成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResult<DutyScheduleExchange> saveDatals( DutyScheduleExchangeSaveVO dutyScheduleExchangeSaveVO, HttpServletRequest request ) {
        DutyScheduleExchange exchange = new DutyScheduleExchange();
        BeanUtils.copyProperties(dutyScheduleExchangeSaveVO,exchange);
        DutyScheduleInfo byId = dutyScheduleInfoRepository.getById(dutyScheduleExchangeSaveVO.getDId());
        DutyScheduleExchange list = dutyScheduleExchangeRepository.getOne(new LambdaQueryWrapper<DutyScheduleExchange>()
                .eq(DutyScheduleExchange::getDId, dutyScheduleExchangeSaveVO.getDId())
                .ne(DutyScheduleExchange::getExStatus,"1"),false);
        if(list==null){
            exchange.setExDay(byId.getDDay());
            exchange.setExUserno(dutyScheduleExchangeSaveVO.getExUserno());
            exchange.setCreateBy(dutyScheduleExchangeSaveVO.getExUserno());
            exchange.setCreateTime(LocalDateTime.now());
            exchange.setUpdateBy(dutyScheduleExchangeSaveVO.getExUserno());
            exchange.setUpdateTime(LocalDateTime.now());
            exchange.setDelFlag(DeleteFlagEnum.NOT_DELETE.getValue());
            exchange.setExStatus("0");
            dutyScheduleExchangeRepository.save(exchange);
        }else {
            exchange.setId(list.getId());
            exchange.setExUserno(dutyScheduleExchangeSaveVO.getExUserno());
            exchange.setUpdateBy(dutyScheduleExchangeSaveVO.getExUserno());
            exchange.setUpdateTime(LocalDateTime.now());
            exchange.setDelFlag(DeleteFlagEnum.NOT_DELETE.getValue());
            exchange.setExStatus("0");
            exchange.setRecVersion(list.getRecVersion());
            dutyScheduleExchangeRepository.updateById(exchange);
        }
        SecurityLog operateLog = new SecurityLog();
        operateLog.setCreateBy(dutyScheduleExchangeSaveVO.getExUserno());
        operateLog.setCreateTime(LocalDateTime.now());
        operateLog.setIpAddress( IpUtils.getIpAddr(request));
        operateLog.setSDescription( JSON.toJSONString( dutyScheduleExchangeSaveVO ) );
        operateLog.setSModule("TBSQLS");
        operateLog.setSTitle("SQTB");
        securityLogService.save(operateLog);
        doMessageSend4StartExchange(exchange,byId);
        return BaseResult.ok(exchange,"申请调班成功");
    }

    @Override
    public BaseResult<Page<DutyScheduleExchangeListVO>> listturels(DutyScheduleExchangeListTureDTO dto) {
        LambdaQueryWrapper<DutyScheduleExchange> Wrapper = new LambdaQueryWrapper<>();
        Wrapper.eq(DutyScheduleExchange::getExRecvUserno,dto.getUserno());
        Wrapper.orderByDesc(DutyScheduleExchange::getExDay);
        List<DutyScheduleExchangeListVO> dutyScheduleExchangeListVOS=new ArrayList<>();
        Page<DutyScheduleExchange> page=dutyScheduleExchangeRepository.page(new Page(dto.getPageNum(),dto.getPageSize()),Wrapper);
        if(!CollectionUtils.isEmpty(page.getRecords())){
            for (DutyScheduleExchange record : page.getRecords()) {
                DutyScheduleExchangeListVO listVO = new DutyScheduleExchangeListVO();
                DutyScheduleInfo byId = dutyScheduleInfoRepository.getById(record.getDId());
                BeanUtils.copyProperties(record,listVO);
                listVO.setDDay(String.valueOf(record.getExDay()));
                if(!StringUtils.isEmpty(byId.getDDeptname())){
                    listVO.setDeptName(byId.getDDeptname());
                }
                dutyScheduleExchangeListVOS.add(listVO);
            }
        }
        Page<DutyScheduleExchangeListVO> pageIno=new Page();
        pageIno.setRecords(dutyScheduleExchangeListVOS);
        pageIno.setTotal(page.getTotal());
        return  BaseResult.ok(pageIno,"查询成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResult<DutyScheduleExchange> turels(LSDutyScheduleExchangetureDTO dto,HttpServletRequest request) throws Exception {
        DutyScheduleExchange exchange = dutyScheduleExchangeRepository.getById(dto.getId());
        exchange.setExStatus("1");
        exchange.setExConfrimTime(LocalDateTime.now());
        exchange.setUpdateBy(dto.getUserno());
        exchange.setUpdateTime(LocalDateTime.now());
        exchange.setDelFlag(DeleteFlagEnum.NOT_DELETE.getValue());
        dutyScheduleExchangeRepository.updateById(exchange);
        DutyScheduleInfo dutyScheduleInfo=dutyScheduleInfoRepository.getById(exchange.getDId());
        dutyScheduleInfo.setDUserno(dto.getUserno());
        List<BasePersonInfo> basePersonInfos = basePersonInfoRepository.list(new LambdaQueryWrapper<BasePersonInfo>()
                .eq(BasePersonInfo::getUserno,dto.getUserno()));
        if (CollectionUtils.isEmpty(basePersonInfos)){
            throw new Exception("无该工号对应人员!");
        }
        basePersonInfos=basePersonInfos.stream().filter(i->i.getDeptId()!=null&&baseDeptInfoRepository.getById(i.getDeptId())!=null).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(basePersonInfos)){
            throw new Exception("无该工号对应人员部门!");
        }
        BasePersonInfo tempBasePerson=basePersonInfos.stream().filter(i->"0".equals(i.getSideLine()))
                .findAny().orElse(null);
        if (tempBasePerson==null){
            tempBasePerson=basePersonInfos.get(0);
        }
        BasePersonInfo basePersonInfo=tempBasePerson;
        BaseDeptInfo baseDeptInfo=baseDeptInfoRepository.getById(basePersonInfo.getDeptId());
        dutyScheduleInfo.setDDeptname(baseDeptInfo.getDeptName());
        dutyScheduleInfo.setDDept(baseDeptInfo.getId());
        dutyScheduleInfo.setDPhone(basePersonInfo.getPhone());
        dutyScheduleInfo.setDUsername(basePersonInfo.getUsername());
        dutyScheduleInfo.setDUserno(basePersonInfo.getUserno());
        dutyScheduleInfo.setUpdateBy(basePersonInfo.getUserno());
        dutyScheduleInfo.setDExchangerName( exchange.getExUsername());
        dutyScheduleInfo.setDUserjob(null);
        String dupostByH4a = getDupostByH4a(basePersonInfo.getRealRackCode());
        dutyScheduleInfo.setDutypost(dupostByH4a);
        dutyScheduleInfo.setRecVersion(baseDeptInfo.getRecVersion());
//        dutyScheduleInfo.setDUserjob(basePersonInfo.getDutypost());
        dutyScheduleInfoRepository.updateById(dutyScheduleInfo);
//        updateDutyScheduleInfo(exchange);
        dutyScheduleExchangeRepository.removeById(exchange);
        SecurityLog operateLog = new SecurityLog();
        operateLog.setCreateBy(dto.getUserno());
        operateLog.setCreateTime(LocalDateTime.now());
        operateLog.setIpAddress( IpUtils.getIpAddr(request));
        operateLog.setSDescription( JSON.toJSONString( dto ) );
        operateLog.setSModule("TBSQLS");
        operateLog.setSTitle("qrtb");
        securityLogService.save(operateLog);

        // 调班发送短信


        return  BaseResult.ok(exchange,"确认成功");
    }

    @Override
    public BaseResult<List<String>> listlsTest() {
        ArrayList<String> list = new ArrayList<>();
        list.add("aaaa");
        list.add("bbbb");
        list.add("cccc");
        list.add("dddd");
        list.add("eeee");
        list.add("ffff");
        return  BaseResult.ok(list,"确认成功");
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateDutyScheduleInfo(DutyScheduleExchange exchange){
        //根据工号找出人员表中被换班人的人员信息
        List<BasePersonInfo> basePersonInfos = basePersonInfoRepository.list(new LambdaQueryWrapper<BasePersonInfo>()
                .eq(BasePersonInfo::getUserno,exchange.getExRecvUserno()));
        BasePersonInfo basePersonInfo=basePersonInfos.get(0);
        DutyScheduleInfo dutyScheduleInfo = new DutyScheduleInfo();
        dutyScheduleInfo.setId(exchange.getDId());
        dutyScheduleInfo.setDUserno(exchange.getExRecvUserno());
//        dutyScheduleInfo.setDUserjob(basePersonInfo.getDutypost());
        dutyScheduleInfo.setRankCode(basePersonInfo.getRankCode());
        String dupostByH4a = getDupostByH4a(basePersonInfo.getRealRackCode());
        dutyScheduleInfo.setDutypost(dupostByH4a);
        dutyScheduleInfo.setDUsername(exchange.getExRecvUsername());
        dutyScheduleInfo.setDPhone(basePersonInfo.getPhone());
        dutyScheduleInfoRepository.updateById(dutyScheduleInfo);
    }
    private String getDupostByH4a(String rankCode) {
        String result = "";
        if (rankCode==null){
            return result;
        }
        switch (rankCode) {
            case "05":
                result = "厅局级正职";
                break;
            case "06":
                result = "厅局级副职";
                break;
            case "07":
                result = "处级人员";
                break;
            case "08":
                result = "处级人员";
                break;
            case "09":
                result = "科长级人员";
                break;
            case "10":
                result = "科长级人员";
                break;
            case "11":
                result = "科员";
                break;
            case "99":
                result = "无行政级别";
                break;
            default:
                return "其他";
        }
        return result;
    }

}
